/**
 * 
 */
package com.template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.template.utils.TStringUtils;

/**   
*    
* 项目名称：admin   
* 类名称：TmodelJs   
* 类描述：   
* 创建人：henry   
* 创建时间：Jul 5, 2019 2:25:55 PM   
* @version        
*/
public class TmodelJs {
	public static void createXMLFile(String tableName, String packageName,List<TableBean>columnList,List<TableBean>priList, List<TableBean>noPriList, String folderName, String path) {
		String className = TStringUtils.tableName2ClassName(tableName);
		String attrName = TStringUtils.columnName2Attr(tableName);
		String str = "";
		str += "//删除按钮\r\n";
		str += "function del"+className+"(id){\r\n";
		str +="	if(confirm(\"你确定要删除吗?\")){\r\n";
		str +="		$.ajax({\r\n";
		str +="		 type:\"GET\",\r\n";
		str +="		 url:_ctx+\"/"+attrName+"/del/\"+id,\r\n";
		str +="		 cache:false,\r\n";
		str +="		 dataType:\"json\",\r\n";
		str +="		 data:{time:new Date().getTime()},\r\n";
		str +="		 success:function(data){\r\n";
		str +="			if(data.code == 200){\r\n";
		str +="				window.location.href=window.location.href;\r\n";
		str +="			}else{\r\n";
		str +="				alert(data.msg);\r\n";
		str +="			}\r\n";
		str +="		}\r\n";
		str +="	 });\r\n";
		str +="	}\r\n";
		str +="}\r\n\r\n";
		str +="//编辑信息\r\n";
		str +="function edit"+className+"(";
		for (int i = 0; i < columnList.size(); i++) {
		 str +=columnList.get(i).getColumnName()+ (i==columnList.size()-1?"){\r\n":",");
		}
		str +="    reload"+className+"Modal(_ctx+\"/"+attrName+"/edit\",";
		for (int i = 0; i < columnList.size(); i++) {
			str +=columnList.get(i).getColumnName()+ ",";
		}
		str +="\"编辑\",\"更改\");\r\n";
		str +="    $(\"#"+attrName+"Modal\").modal(\"show\");\r\n";
		str +=" }\r\n\r\n";
		str +="function reload"+className+"Modal(url,";
		for (int i = 0; i < columnList.size(); i++) {
			str +=columnList.get(i).getColumnName()+ ",";
		}
		str +="title,btnText){\r\n";
		str +="      $(\"input[name='url']\").val(url);\r\n";
		for (int i = 0; i < columnList.size(); i++) {
		str +="      $(\"input[name='"+columnList.get(i).getColumnName()+"']\").val("+columnList.get(i).getColumnName()+");\r\n";
		}
		str +="      $(\"#"+attrName+"modelHead\").text(title);\r\n";
		str +="      $(\"#submit-"+attrName+"Btn\").text(btnText);\r\n";
		str +="}\r\n\r\n";
        str +="$(function(){\r\n";
        str +="    //提示框\r\n";
        str +="    $(\"[data-toggle='tooltip']\").tooltip();\r\n";
        str +="    //表哥分页\r\n";
        str +="    $('#"+attrName+"List').DataTable({\r\n";
        str +="           'paging'      : true,\r\n";
        str +="           'lengthChange': true,\r\n";
        str +="           'ordering'    : true,\r\n";
        str +="           'info'        : true,\r\n";
        str +="           'autoWidth'   : false,\r\n";
        str +="           'scrollX'	    : true,\r\n";
        str +="           'pagingType'  : 'full_numbers',\r\n";
        str +="           'pageLength'  : 10\r\n";
        str +="    });\r\n\r\n";
        str +="$(\"#add"+className+"\").click(function(){\r\n";
        str +="reload"+className+"Modal(_ctx+\"/"+attrName+"/add\",";
		for (int i = 0; i < columnList.size(); i++) {
			str +="\"\",";
		}
		str += "\"新增\",\"新增\");\r\n";
		str += "  $(\"#"+attrName+"Modal\").modal(\"show\");\r\n";
		str += "});\r\n\r\n";
        str += "//角色模态框的提交按钮\r\n";
		str +="$(\"#submit-"+attrName+"Btn\").click(function(){\r\n";
		for (int i = 0; i < columnList.size(); i++) {
			str +="var "+columnList.get(i).getColumnName()+" = $(\"input[name='"+columnList.get(i).getColumnName()+"']\").val();\r\n";
		}
        str +="var url = $(\"input[name='url']\").val();\r\n";
        str +="$.ajax({\r\n";
        str +="  type:\"POST\",\r\n";
        str +="  cache:false,\r\n";
        str +="  url:url,\r\n";
        str +="  dataType:\"json\",\r\n";
        str +="  data:{";
        for (int i = 0; i < columnList.size(); i++) {
        str +=columnList.get(i).getColumnName()+":"+columnList.get(i).getColumnName()+ (i==columnList.size()-1?"},\r\n":",");	
        }
        str +="   success:function(data){\r\n";
        str +="       if(data.code == 200){\r\n";
        str +="                window.location.href=window.location.href;\r\n";
        str +="                $(\"#"+attrName+"Modal\").modal(\"hide\");\r\n";
        str +="       }else{\r\n";
        str +="           alert(data.msg);\r\n";
        str +="       }\r\n";
        str +="   }\r\n";
        str +=" });\r\n";
        str +=" });\r\n";
        str +="})\r\n";

		try {
			String allPath = path+"\\"+attrName+".js";
			OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(allPath),"UTF-8");
			output.write(str);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
